<?php
/**
 * $Id: asset.php 146 2010-01-11 12:56:26Z shrey $
 * 
 * This file can not be redistributed to anyone without the permission of Giga Promoters
 * Copyright 2005 - 2008, Giga Promoters
 * 						  608 Aggarwal Cyber Plaza
 *						  Netaji Subhash Place, Delhi - 110034
 *						  www.gigapromoters.com
 * 
 *
 * This is parent of parent class of a controller and should not be edited ever without permission.
 * @version 			$Rev: 146 $
 * @lastModifiedBy		$LastChangedBy: shrey $
 * @lastChanged			$Date: 2010-01-11 18:26:26 +0530 (Mon, 11 Jan 2010) $
 * @copyright 			Giga Promoters (www.gigapromoters.com)
 * 
 */

/**
 * Asset helper to help display media of an item.
 *
 */
class AssetHelper extends AppHelper {

	var $helpers = array('Html','Effects.Thickbox');

	/**
	 * This is the only function which should be used to show a preview of any media content.
	 * also pass field data only not with model alias.
	 *
	 * @param unknown_type $asset
	 * @param unknown_type $directory
	 * @param unknown_type $options
	 * @return unknown
	 */
	function preview($asset, $options = array())
	{
		if(!$type = $this->_getType($asset))
		{
			return __("Invalid Asset type or Asset type not found",true);
		}
		$previewFunc = 'preview'.$type;
		$callbackFn = isset($options['callback']) ?  $options['callback'] : null;
		unset($options['callback']);
		$output = $this->$previewFunc($asset, $options);
		if($callbackFn && function_exists($callbackFn))
		{
			$output = call_user_func_array($callbackFn,array($output,$asset,$this->Html));
		}
		return $output;
	}

	function _getType($asset)
	{
		$parts = explode('.', low($asset['name']));
		$type = null;
		if(isset($asset['upload_type']))
			$type = $asset['upload_type'];
		else
		{
			$extension = array_pop($parts);
			if(in_array($extension, array('jpg','jpeg','gif','png')))
			{
				$type = 'Image';
			}
			elseif(in_array($extension, array('avi','mpg','mpeg','3gp','wmv', 'flv'))) {
				$type = 'Video';
			}
		}
		return @$type;
	}
	/**
	 * Return 's 
	 * @param unknown_type $asset
	 * @param unknown_type $options
	 * @return unknown_type
	 */
	function previewImage($asset,  $options,$callBackFunc = null)
	{
		$fileUrl = isset($asset['file_url']) ? $asset['file_url'] : '/uploads/'.$asset['name'];
		if(isset($options['thickboxPreview']))
		{
			$options['thickboxPreview'] = am(array('height'=>'500','width'=>500),$options['thickboxPreview']);
			$this->Thickbox->setProperties(array('id'=>'image-'.$asset['id'], 'height'=>$options['thickboxPreview']['height'], 'width'=>$options['thickboxPreview']['width']));
			$attrOptions = array_diff_key($options,array('thickboxPreview'=>'')); 
			$this->Thickbox->setPreviewContent($this->Html->image($fileUrl,$attrOptions));
			$this->Thickbox->setMainContent($this->Html->image($fileUrl,array('thumb'=>array('height'=>$options['thickboxPreview']['height'], 'width'=>$options['thickboxPreview']['width']))));
			$content = $this->Thickbox->output();
		}
		else 
		{
			$content = $this->Html->image($fileUrl,$options);
		}
		return $content;
	}
	/**
	 * Renders element for video preview.
	 * @param $asset
	 * @param $options
	 * @return unknown_type
	 */
	//@todo check for proper functioning.-shrey.
	function previewVideo($asset,  $options)
	{
		$view = ClassRegistry::getObject('View');
		if(isset($options['thickboxPreview']))
		{
			$out = $view->element('video_thickbox', array('options'=>$options,"asset" =>$asset));
		}
		else {
			$out = $view->element('video', array('asset'=>$asset, 'options'=>$options));
		}

		return $out;
	}

}
?>